clear
set more off
macro drop all
capture log close

/********************************************************************************
Discrimination in Multi-Phase Systems: Evidence from Child Protection
Clean Child Level Data

Created on: 11/28/17

Last Modified on: 2/20/2024

Description: This file cleans the raw child level data.

Note that we have removed the file directory names from this program for 
confidentiality reasons.
********************************************************************************/

** Setting the Directory
global rawdata 
global cleandata
global tmp 

/********************************************************************************

The raw child level data is already unique at the child level, so I just clean
up some of the variables here.

*******************************************************************************/

**Load raw case data
"use ${rawdata}child_level.dta", clear

**Rename, label and clean variables
rename intakechildvicpartyid vicid
la var vicid "Child Victim ID"

gen female=(gender=="female")
replace female=. if gender==""
la var female "Female"
drop gender

gen dob=date(birth_dt, "YMD")
format dob %d
la var dob "Victim DOB"
drop birth_dt

gen racecat=.
egen racesum=rowtotal(amerin asian black hawpac white)
replace racecat=1 if racesum>1 & racesum!=. 
replace racecat=2 if amerin==1 & racecat==.
replace racecat=3 if asian==1 & racecat==.
replace racecat=4 if black==1 & racecat==.
replace racecat=5 if hawpac==1 & racecat==.
replace racecat=6 if white==1 & racecat==.
la var racecat "Race Category"
la def racecat 1 "Multiracial" 2 "American Indian/Alaska Native" 3 "Asian" 4 ///
	"Black" 5 "Hawaiian/Pacific Islander" 6 "White"
la val racecat racecat
drop amerin asian black hawpac white racesum

gen multirace=(racecat==1)
gen amerin=(racecat==2)
gen asian=(racecat==3)
gen black=(racecat==4)
gen haw=(racecat==5)
gen white=(racecat==6)

foreach x in multirace amerin asian black haw white {
	replace `x'=. if racecat==.
}

la var multirace "Victim is Multiracial"
la var amerin "Victim is American Indian/Alaska Native"
la var asian "Victim is Asian"
la var black "Victim is Black"
la var haw "Victim is Hawaiian/Pacific Islander"
la var white "Victim is White"

compress 
sort vicid
save "${cleandata}child_level_clean.dta", replace





















